TWB.New_Module({
	///////////////////////////
	name : "Jscripts",       //
	author : "enduo",        //
	version : 1.0,           //
	///////////////////////////
	
	settings : {
		js_savedir : {
			type : "text",
			onclick : function(){
				var temp=TWB.Modules.Jscripts.PickDir();
				if(temp!=null){
					this.value=temp;
				}
			}
		}
	},
	PickDir : function(){
		var fp=TWB.Cc["@mozilla.org/filepicker;1"].createInstance(TWB.Ci.nsIFilePicker);
		fp.init(window,TWB.lang("changedir"),TWB.nsIFilePicker.modeGetFolder);
		if(!fp.show()){
			return fp.file.path;
		}
		return null;
	},
	Load_Scripts : function(){
		TWB._JSCRIPTS={};
		// Load Script Files If plugin exists
		var loc=TWB.get_var("js_savedir",TWB.DEFAULTS.js_savedir);
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		var fk = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
		fk.initWithPath(loc);
		if(fk.exists()){
			var fsk=fk.directoryEntries;
			// Restore original state
			var CX=TWB.gei("TWB-JScriptList").childNodes;
			for(l=CX.length-1; l>=0; l--){
				if(CX[l].id!="TWB-JScriptList-Refresh"){
					CX[l].parentNode.removeChild(CX[l]);
				}
			}
			var BX=TWB.dce("menuseparator");
			TWB.gei("TWB-JScriptList").appendChild(BX);
			while(fsk.hasMoreElements()){
				var cutz=fsk.getNext();
				// Skip directories
				var cutz_=cutz.QueryInterface(Components.interfaces.nsILocalFile);
				if(cutz_.isDirectory() || !cutz_.path.match(".js")){
					continue;
				}
				// Add new
				var ist = TWB.Cc["@mozilla.org/network/file-input-stream;1"].createInstance(TWB.Ci.nsIFileInputStream);
				ist.init(cutz,0x01,00004,null);
				var szt = TWB.Cc["@mozilla.org/scriptableinputstream;1"].createInstance(TWB.Ci.nsIScriptableInputStream);
				szt.init(ist);
				var ctsz = szt.read(szt.available());
				szt.close();
				try{
					var Nz=ctsz.match(/\@Name\s=\s(.+)/);
					Nz=(Nz && Nz.length>1)?Nz[1]:"";
					var S=ctsz.match(/\@Shortcut\s=\s(.+)/);
					S=(S && S.length>1)?S[1]:"";
					var T=ctsz.match(/\@TWB\s=\s(.+)/);
					T=(T && T.length>1)?T[1]:"false";
					if(Nz!=""){
						// Populate scripts obj
						if(typeof TWB._JSCRIPTS[Nz]=="undefined"){
							TWB._JSCRIPTS[Nz]={src:ctsz,include:T};
							// Assign Shortcut
							TWB.Modules.Tools.Add_KeyboardShortcut(S,"TWB.Modules.Jscripts.Run('"+Nz+"');",true);
							// Add button
							var X=TWB.gei("TWB-JScriptList");
							var A=TWB.dce("menuitem");
							A.setAttribute("id","TWB_Scripts_"+Nz);
							A.setAttribute("label",Nz);
							A.setAttribute("oncommand","TWB.Modules.Jscripts.Run('"+Nz+"');");
							X.appendChild(A);
						}
						else{
							// Log double script name + Name + Path
							log("","Found scripts using same name \""+Nz+"\" in "+pth,2);
						}
					}
					else{
						// Log no name for script + path
						log("","No script name found in "+pth,2);
					}
				}catch(e){}
			}
		}
	},
	Run : function(scid){
		try{
			var scsrc=TWB._JSCRIPTS[scid].src;
			var include=TWB._JSCRIPTS[scid].include;
			if(include=="true"){
				eval(scsrc);
			}
			else{
				var zew=TWB._window();
				if(zew){
					if(typeof zew.wrappedJSObject!="undefined"){
						zew=zew.wrappedJSObject;
					}
					var sc=zew.document.createElement("script");
					sc.type='text/javascript';
					var txt=zew.document.createTextNode(scsrc);
					sc.appendChild(txt);
					zew.document.body.appendChild(sc);
				}
				else{
					alert(TWB.lang("ure"));
				}
			}
		}catch(e){
			log(e,"JScript");
		}
	},
	priority : 1000,
	init : function(){
		TWB.Modules.Jscripts.Load_Scripts();
		TWB.Modules.Tools.Reload_KeyboardShortcuts();
	},
});
